<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        $this->model = new \app\admin\model\Exam_res;
    }  

      /**
     * 首页获取数据
     * @ApiSummary  首页获取数据
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/index)
     * @ApiReturnParams   (name="index", type="array", required=true, description="首页上面的数据")
     * @ApiReturnParams   (name="test_questions", type="array", required=true, description="首页下面的数据：试题区")
     * 
     */
    public function index(){

        $index = Db::name('category')
        ->field('id,name,image')
        ->where('type','=','index')
        ->where('pid','=',0)
        ->order('weigh desc')
        ->select();

        $test_questions = Db::name('category')
        ->field('id,name,image')
        ->where('type','=','test questions')
        ->where('pid','=',0)
        ->order('weigh desc')
        ->select(); 
        
        $data['index'] = $index;
        $data['test_questions'] = $test_questions;

        $this->success('请求成功',$data);
    }

    /**
     * 提交反馈
     * @ApiSummary  
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/tips)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token") 
     * @ApiParams   (name="content", type="string", required=true, description="反馈意见")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="提交成功与否")
     */
    public function tips()
    {
        $content = $this->request->request('content');
        if(empty($content))$this->error('您提交的意见为空');
        $Tips = new \app\admin\model\Tips;
        $insert['content'] = $content;
        $insert['user_id'] = $this->user_id;

        $res = $Tips->allowField(true)->save($insert);

        if($res){$this->success('提交成功');}else{$this->error('提交失败');}
    }

    /**
     * 激活课程，获取更多次数
     * @ApiSummary  
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/active)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token") 
     * @ApiParams   (name="code", type="string", required=true, description="激活码")
     * @ApiReturnParams   (name="msg", type="string", required=true, description="操作成功与否的信息")
     */
    public function active()
    {
        $code = $this->request->request('code');
        if(empty($code))$this->error('请提交激活码');

        $data = Db::name('code')->where(['code'=>$code,'status'=>0])->find();
        if(empty($data))$this->error('对不起，没有找到对应的激活码');

        $res = Db::name('code')->where(['id'=>$data['id']])->update(['status'=>1,'updatetime'=>time(),'user_id'=>$this->user_id]);
        if($res){$this->success('激活成功');}

    }

        /**
     * 激活过的章节
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/active_history)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token") 
     * @ApiReturn   ({
        {
          "code": 1,
          "msg": "激活过的章节",
          "time": "1587542239",
          "data": [
            {
              "cchaptername": "2018考前押题密卷二"
            },
            {
              "cchaptername": "2018考前押题密卷三"
            },
            {
              "cchaptername": "2020真题模拟卷"
            },
            {
              "cchaptername": "2020真题模拟试卷一"
            }
          ]
        }
     })
     */
    public function active_history()
    {
        $code = Db::name('code')->where(['user_id'=>$this->user_id])->field('category_id')->select();

        $return_data = [];
        foreach ($code as $key => $value) {
          $array = Db::name('category')->where(['id'=>['in',$value['category_id']]])->field('name as cchaptername')->select();
          $return_data = array_merge($array,$return_data);
        }
        $this->success('激活过的章节',$return_data);
        
    }

     /**
     * 专属老师
     * @ApiSummary  
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/teacher)
     * @ApiReturnParams   (name="image", type="string", required=true, description="二维码照片")
     * @ApiReturnParams   (name="wechat", type="string", required=true, description="微信号码")
     */
    public function teacher()
    {
        $res = Db::name('teacher')->find();
        $this->success('教师微信',$res);

    }
  

}
